/**
 * 与抢单页面相同的样式，都来自rush.scss
 * @author levy
 * @date    2016/1/10
 * @description []
 */
import RushHeader from "../components/RushHeader";
import NoData from "../components/NoData";
import PullRefresh from "../components/mui/PullRefresh";
import {health} from "../const/offices";
import {prefix} from "../const/prefix";


liefeng.bridgeJs.bridgeReady(function ( lf, r) {
    var ajax = lf.utils.ajax,
        RC = r.createClass,
        api = health.api,
        apiUrl = prefix.oldpeople,
        doAction = lf.bridgeJs.doAction,
        appAlert = lf.bridgeJs.alert,
        dataId,
        log = lf.utils.log;

    var SignFor = React.createClass({
        getInitialState: function () {
            return {
                visible: ''
            }
        },
        signFor: function (e) {
            lf.utils.stopBubble(e);
            var dataRes = this.props.data;
                ajax({
                    url:apiUrl + api.doRush,
                    type:'POST',
                    data: {
                        wfTaskId: dataRes.taskId,
                        staffid: dataId.id,
                    },
                    headers: {
                        openId: dataId.openId,
                    },
                    success: function (data) {
                        if(data.code == '200'){
                            mui.toast('签收成功');
                            this.setState({
                                visible: 'ele-hide'
                            });
                            RushHeader.updateCount();
                        }else{
                            appAlert(data.desc);
                        }
                    }.bind(this)
                });
        },

        componentDidMount: function () {
            r.findDOMNode(this.refs.toDetail).addEventListener('tap', function (e) {
                var data = this.props.data;
                doAction('order-detail-oa.html?flag=sign&orderId='+data.orderId+"&taskId="+data.taskId, '签收详情')
            }.bind(this));

            r.findDOMNode(this.refs.signFor).addEventListener('tap', function (e) {
                this.signFor(e);
            }.bind(this))
        },
        render: function () {
            var data = this.props.data;
            return (
                <li className={"mui-table-view-cell " + this.state.visible}>
                    <a  ref="toDetail" className="mui-navigate-right">
                        <span className="work-title">{data.title}</span>
                        <button ref="signFor" className="rush-btn oa-btn lf-green-outlined-btn">签收</button>
                    </a>
                </li>
            )
        }
    });

	var SignCon = React.createClass({
        getInitialState:function(){
            return ({
                header:{
                    text:'待签收',
                    num:0,
                    monthlyProcessed:0
                },
                oaColor: 'oa-title',
                noData:{
                    span_text:'本月还没有签收信息',
                    btn_text:'查看工单处理',
                    page_url:'task-processing-oa.html',
                    page_title:'工单处理',
                },
                total:1,
                signForList:[],
            })
        },
        getHeader(user) {
            var me = this;
            ajax({
                url: apiUrl + api.listHeader,
                data:{actor: dataId.id},
                headers: {
                    openId: dataId.openId,
                },
                success: (data) => {
                    data = data.data;
                    me.setState({
                        header:{
                            text: '待签收',
                            num: data.waitSignCount || 0,
                            monthlyProcessed: data.monthCompleteCount || 0
                        }
                    })
                }
            });

        },
        getList(user) {
            var me = this;
            ajax({
                url: apiUrl + api.signForList,
                data:{
                    actor: dataId.id,
                    page:1,
                    size:999,
                },
                headers: {
                    openId:dataId.openId,
                },
                success: (data) => {
                    var list = data.dataList.map(function(data){
                        return <SignFor data={data}/>
                    });
                    me.setState({
                        signForList:list,
                        total:list.length,
                    })
                }
            });

        },
        componentWillMount:function(){
            this.getHeader(this.props.user);
            this.getList(this.props.user);

            lf.appJs.refresh(user => {
                this.getHeader(user);
                this.getList(user);
            });
        },
		render: function () {
            var total = this.state.total;

            return (
                <div className="rush ">
                    <RushHeader data={this.state.header} oaTitle={this.state.oaColor}/>
                    <div className="lf-list">
                        <ul className="mui-table-view">
                            {this.state.signForList}
                        </ul>
                    </div>
                    {total > 0 ? '' :  <NoData data={this.state.noData}/>}
                </div>
            );
		}
	});

    var Body = RC({
        _conRender: function () {
            return (
                <SignCon  user={this.props.user}/>
            )
        },
        render: function () {
            return (
                <PullRefresh conRender={this._conRender} />
            )
        }
    });

    lf.appJs.getUser(function (user) {
        ajax({
            url: apiUrl + api.getGlobalId,
            type: 'post',
            data: {
                custGlobalId: user.globalId || user.custLoginVo.globalId,//user.id,'C1606281657108af41b8e068927' ||
            },
            success: function (data) {
                dataId = data.data;
                r.render(
                    <Body  user={user}/>,
                    document.getElementById('root')
                )
            }
        })
    });
});
